<template>
  <el-container>
    <el-header>
      <!-- 顶部 -->
      <img src="../assets/header.jpg" alt />
      <p>电商管理系统</p>
      <el-button type="info" @click="exit">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧部 -->
      <el-aside :width="isshrinkage?'64px':'200px'">
        <!-- 侧边栏折叠 -->
        <div class="aside-button" @click="shrinkage">|||</div>
        <!-- 侧边栏 -->
        <el-menu
          default-active='/users'
          :collapse-transition="false"
          :collapse="isshrinkage"
          router
          unique-opened
          background-color="#606358"
          text-color="#fff"
          active-text-color="#21A1FF"
        >
          <el-submenu :index="String(item.id)" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconslist[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+String(subitem.path)"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="saveState"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                {{subitem.authName}}
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist: [],
      iconslist: {
        "125": "iconfont icon-users",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao",
      },
      isshrinkage: false, //是否收缩
    };
  },
  created() {
    this.GetMenuList();
  },
  methods: {
    //退出功能
    exit() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取侧边栏数据
    async GetMenuList() {
      const { data: res } = await this.$axios.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    // 左侧菜单收缩
    shrinkage() {
      this.isshrinkage = !this.isshrinkage;
    },
    // 保存链接激活状态
    saveState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
    }
  },
};
</script>
<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    background-color: #7c7e77;
    display: flex;
    align-items: center;
    padding-left: 0;
    color: #fff;
    font-size: 30px;
    letter-spacing: 5px;
    .el-button {
      position: absolute;
      right: 50px;
    }
    img {
      width: 50px;
      height: 50px;
      opacity: 0.5;
    }
    p {
      margin-left: 50px;
    }
  }
  .el-aside {
    background-color: #606358;
    .aside-button {
      background-color: #475161;
      color: #fff;
      text-align: center;
      font-size: 13px;
      line-height: 25px;
      letter-spacing: 2px;
      cursor: pointer;
    }
    .el-menu {
      border-right: 0;
    }
  }
  .el-main {
    background-color: #e9edf1;
  }
  .iconfont {
    margin-right: 10px;
  }
}
</style>